//服务端  管理员修改
#include "staff.h"  
int process_admin_modify_request(int newfd,Msg *msg)
{
	printf("------------%s-----------%d.\n",__func__,__LINE__);
	char sql[DATALEN] = {0};
	char *errmsg;
	char **result;
	int nrow,ncolumn;
	time_t t;
	char buf_msg[120] = "";
	char buf_time[512] = "";
	char buf[1024] = "";
	sprintf(sql,"select *from usrinfo where id=%d",msg->info.id);
	printf("-----------------------------------------\n");
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){
		printf("---****----%s.\n",errmsg);
		if(nrow == 0){
			sprintf(msg->recvmsg,"没有找到工号%d\n",msg->info.id);
			send(newfd,msg,sizeof(Msg),0);
			exit(1);
		}
	}else{			
		time(&t);
		bzero(buf_msg,sizeof(buf_msg));
		bzero(buf_time,sizeof(buf_msg));
		bzero(buf,sizeof(buf_msg));

		switch(msg->flags){	
		case AGE:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改年龄为%d",msg->info.name,msg->info.age);
			sprintf(buf_time,"%s",(char*)ctime(&t));//ctime :把日期和时间转换成字符串
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set age = %d where id = %d",msg->info.age,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}
			break;

		case ADDR:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改住址为%s",msg->info.name,msg->info.address);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set address = '%s' where id = %d",msg->info.address,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}
			break;
		case PHONE:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改手机号码为%s",msg->info.name,msg->info.telphone);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set telphone = '%s' where id = %d;",msg->info.telphone,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}
			break;
		case PASSWORD:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改密码为%s",msg->info.name,msg->info.passwd);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set passwd = '%s' where id = %d",msg->info.passwd,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}
			break;
		case NAME:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改姓名为%s",msg->info.name,msg->info.name);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set name = '%s' where id = %d",msg->info.name,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}
			break;
		case WORK:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改职位为%s",msg->info.name,msg->info.position);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set position = '%s' where id = %d",msg->info.position,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}
			break;
		case SALARY:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改薪资为%lg",msg->info.name,msg->info.salary);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set salary = %lg where id = %d",msg->info.salary,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}
			break;
		case DATE:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改入职日期为%s",msg->info.name,msg->info.data_entry);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set data_entry = '%s' where id = %d",msg->info.data_entry,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}
			break;
		case LEVEL:
			bzero(sql,sizeof(sql));
			sprintf(buf_msg,"管理员%s修改员工评级为%d",msg->info.name,msg->info.rate);
			sprintf(buf_time,"%s",(char*)ctime(&t));
			sprintf(buf,"insert into historyinfo values ('%s','%s');",buf_time,buf_msg);
			sprintf(sql,"update  usrinfo set rate = %d where id = %d",msg->info.rate,msg->info.id);
			sqlite3_get_table(db,buf,&result,&nrow,&ncolumn,&errmsg);
			if(sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg) != SQLITE_OK){
				printf("---****----%s.\n",errmsg);
				sprintf(msg->recvmsg,"修改失败\n");
				send(newfd,msg,sizeof(Msg),0);
				exit(1);
			}			
			break;
		}
	}
	strcpy(msg->recvmsg,"修改成功");
	send(newfd,msg,sizeof(Msg),0);
	return 0;
}

